package com.aj.service;

import com.aj.Interface.MenuService;
import com.aj.dao.MenuDao;
import com.aj.pojo.Menu;
import com.aj.pojo.Permission;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;


    //分页查询
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.findPage(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

   public List<Permission> findAll() {        return menuDao.findAll();    }
    //添加菜单
    @Override
    public void add(Menu menu, String parentMenu) {
        if(parentMenu != null){
            Integer id = menuDao.findMenuIdByName(parentMenu);
            menu.setParentMenuId(id);
        }
        menuDao.add(menu);
    }

    //删除菜单
    @Override
    public void delete(Integer id) {
        Integer count = menuDao.findCountByMenuId(id);
        if(count>0){
            throw new RuntimeException("该菜单被引用,不能删除");
        }
        menuDao.delete(id);
    }

    //修改菜单(根据id查一个对象,回显数据)
    @Override
    public Menu findById(Integer id) {
        Menu menu = menuDao.findById(id);
        if (menu.getParentMenuId()!=null){
            String parentMenuName = menuDao.findNameById(menu.getParentMenuId());
            menu.setParentMenuName(parentMenuName);
        }
        return menu;
    }
    //修改菜单
    @Override
    public void update(Menu menu) {
        menuDao.update(menu);

    }
}
